//
//  HTCopilotManageViewController.m
//  TruckDriver
//
//  Created by Ting Han on 2017/9/26.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTCopilotManageViewController.h"
#import "HTTitleView.h"
#import "HTUnbindCopilotCell.h"
#import "HTInviteCopilotController.h"
#import "HTCopilotCell.h"


@interface HTCopilotManageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *safeBottomHeight;
@property (nonatomic,strong) NSMutableArray *dataSource1;
@property (nonatomic,strong) NSMutableArray *dataSource2;
@end

@implementation HTCopilotManageViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"副驾管理";
    [self configTable];
  
}
- (void)configTable{
    self.safeBottomHeight.constant = IPHONE_SAFEBOTTOMAREA_HEIGHT;
    self.tableView.width = kHTScreenWidth;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"HTUnbindCopilotCell" bundle:nil] forCellReuseIdentifier:unbindCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"HTCopilotCell" bundle:nil] forCellReuseIdentifier:UnbindCopilotCell];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = kBackgroundColor;
    [self configTableHeader];
  //  [self configTableFooter];
    self.tableView.mj_header = self.header;
  //  self.tableView.mj_footer = self.footer;
    self.noDataView = [[HTNoDataView alloc]initWithFrame:self.tableView.bounds];
    self.noDataView.showMessage = @"暂无副驾";
}

- (IBAction)didClickInvitedCopilot:(UIButton *)sender {
    HTInviteCopilotController *invite = [[HTInviteCopilotController alloc]init];
    [self.navigationController pushViewController:invite  animated:YES];
}

- (NSMutableArray *)dataSource1{
    if (!_dataSource1) {
        _dataSource1 = [NSMutableArray array];
    }
    return _dataSource1;
}
- (NSMutableArray *)dataSource2{
    if (!_dataSource2) {
        _dataSource2 = [NSMutableArray array];
    }
    return _dataSource2;
}
#pragma mark --- tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     [tableView tableViewDisplayView:self.noDataView ifNecessaryForRowCount:self.dataSource1.count + self.dataSource2.count];
    if (section == 0) {
        return self.dataSource1.count;
    }
    if (section == 1) {
        return self.dataSource2.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        HTCopilotCell *cell = (HTCopilotCell*)[tableView dequeueReusableCellWithIdentifier:UnbindCopilotCell forIndexPath:indexPath withController:self];
        cell.unbindModel = self.dataSource1[indexPath.row];
        __weak typeof(self) weakSelf = self;
        cell.detialBlock = ^{
            [weakSelf.tableView.mj_header beginRefreshing];
        };
        return cell;
    }
    if (indexPath.section == 1) {
        HTUnbindCopilotCell *cell = (HTUnbindCopilotCell*)[tableView dequeueReusableCellWithIdentifier:unbindCell forIndexPath:indexPath withController:self];
        cell.unbindModel= self.dataSource2[indexPath.row];
        __weak typeof(self) weakSelf = self;
        cell.detialBlock = ^{
            [weakSelf.tableView.mj_header beginRefreshing];
        };
        return cell;
    }
    return nil;
  
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
         return [self headViewWithTitle:@"待处理"];
    }
    if (section == 1) {
         return [self headViewWithTitle:@"我的副驾"];
    }
    return nil;
}
- (UIView *)headViewWithTitle:(NSString *)message{
    HTTitleView *titleView = [[HTTitleView alloc]initWithFrame:CGRectMake(0, 0, kHTScreenWidth, 50) withTitle:message];
    titleView.backgroundColor = kBackgroundColor;
    return titleView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 125;
    }
    return 89;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 && self.dataSource1.count <= 0) {
        return 0;
    }
    if (section == 1 && self.dataSource2.count <= 0) {
        return 0;
    }
    return 44;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 44; //sectionHeaderHeight
        if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

#pragma mark --- 网络请求
- (void)loadNewData{
    [self netLoadCopilotList];
}
//- (void)loadMoreData{
//    [self.tableView.mj_footer endRefreshing];
//    self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
//}
- (void)netLoadCopilotList{
    
    [HTHttpRequestManager requestCopilotssuccess:^(id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        DBLog(@"我的副驾：%@",responseObject);
        if (!responseObject) {
            [HTShowUtil showMessage:@"获取失败"];
            return ;
        }
        NSString *codeStr = [NSString stringWithFormat:@"%@",responseObject[@"code"]];
        if (![codeStr isEqualToString:@"200"]) {
            [HTShowUtil showMessage:@"获取失败"];
            return;
        }
        self.dataSource1 = [HTUnbindCopilotModel mj_objectArrayWithKeyValuesArray:responseObject[@"unchanger"][@"data"]];
        self.dataSource2 = [HTUnbindCopilotModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
        [self.tableView reloadData];
        
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [HTShowUtil showMessage:@"获取失败"];
    }];
   
}

@end
